package com.ruoyi.api.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.cache.ICacheService;
import com.ruoyi.api.user.requestVo.UserQQLoginRequestVo;
import com.ruoyi.api.util.JWTUtil;
import com.ruoyi.api.util.wx.AuthUtil;
import com.ruoyi.api.util.wx.DownloadPicFromURL;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.service.IUserService;

import io.swagger.annotations.ApiOperation;

/**
 * APP微信授权操作处理
 * @author ruoyi
 * @date 2018-12-14
 */
@Controller
@RequestMapping("/api/qq")
public class ApiQQController extends BaseController
{
	
	private static final Logger log = LoggerFactory.getLogger(ApiQQController.class);
	
    private String prefix = "api:qq";
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private ICacheService cacheService;
	
	
	/**
	 * QQ授权登录
	 * @return
	 */
	@Log(title = "QQ授权登录", businessType = BusinessType.LOGIN)
	@PostMapping("/login")
	@ResponseBody
	@ApiOperation(value="AQQ授权登录", notes="AQQ授权登录",produces = "application/json")
	public HashMap<String, Object> login(@RequestBody UserQQLoginRequestVo loginRequestVo)
	{
		User userOld = new User();
		userOld.setOpenid(loginRequestVo.getOpenid());
		userOld.setSource(2);
		List<User> userList = userService.selectUserList(userOld);
		if(userList!=null&&userList.size()>0){
			User userCurrent = userList.get(0);
			String subject = userCurrent.getOpenid();
			cacheService.put("ry-user-id",subject,userCurrent.getId());
			String jwtToken = JWTUtil.createToken(subject);
			cacheService.put("ry-user-token",subject,jwtToken);
			String result_str = "Bearer:" + jwtToken;
			return successApp(result_str);
		}else{
			User user = new User();
			user.setAccount(loginRequestVo.getOpenid());
			user.setOpenid(loginRequestVo.getOpenid());
			user.setNickName(loginRequestVo.getNickName());
			if(loginRequestVo.getHeadPic()!=null&&!"".equals(loginRequestVo.getHeadPic())){
				String headPic = DownloadPicFromURL.downloadPicture(loginRequestVo.getHeadPic());
				user.setHeadPic("app/img/"+headPic);
			}
			user.setSex(loginRequestVo.getSex());
			user.setStatus(1);
			user.setSource(2);
			user.setCreateTime(new Date());
			user.setIsVip(0);
			userService.insertUser(user);
			User param1 = new User();
			param1.setOpenid(loginRequestVo.getOpenid());
			param1.setSource(2);
			List<User> userList1 = userService.selectUserList(param1);
			if(userList1!=null&&userList1.size()>0){
				String subject = loginRequestVo.getOpenid();
				cacheService.put("ry-user-id",subject,userList1.get(0).getId());
				String jwtToken = JWTUtil.createToken(subject);
				cacheService.put("ry-user-token",subject,jwtToken);
				String result_str = "Bearer:" + jwtToken;
				return successApp(result_str);
			}
		}
		return errorApp("qq-0001", "QQ授权登录失败");
	}
	
}
